let jweixin = require('jweixin-module')
export const Appwx_pay = (item) => { //app支付
	uni.login({
		provider: 'weixin',
		onlyAuthorize: true,
		success: (loginRes) => {
			this.post('api/wechat/payment', { //后台接口
				order_id: order_id,
				openid: loginRes.code
			}).then((resinfo) => {
				this._pay(item)
			})
		}
	})
}
export const H5wx_pay = (item) => { //h5支付
	let appid = 'wx9781ae10804e1ba5'
	const code = this.getUrlCode("code")
	const local = window.location.href + '&wx=1'
	if (code == null || code == "") {
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid +
			"&redirect_uri=" + encodeURIComponent(local) + "&response_type=code" +
			"&scope=snsapi_userinfo&state=1#wechat_redirect";
	} else {
		this.getOpenId(item)
	}
}
export const wx_pay = (data) => {//微信小程序支付
	uni.getProvider({
		service: 'payment',
		success: (reson) => {
			order(data).then(res => {
				if (res.code == 0) {
					uni.requestPayment({
						provider: reson.provider[0],
						timeStamp: result.timeStamp,
						nonceStr: result.nonceStr,
						package: result.package,
						paySign: result.paySign,
						signType: result.signType,
						success: (res) => {
							uni.showToast({
								title: '支付成功',
								icon: 'success',
								duration: 2000
							});
						},
						fail: (rest) => {
							uni.showToast({
								title: '支付失败',
								icon: 'error',
								duration: 2000
							});
						}

					})
				}
			})
		}
	})
}
export const getOpenId = (code, item) => { //通过后台获取openid
	this.post('api/wechat/userinfo', item).then(res => {
		this.getKey(res.data.openid, item.order_id);
	})
}
export const getKey = (item) => {
	this.post('api/wechat/H5payment', item).then(res => {
		let info = res.data
		jweixin.config({
			debug: false,
			appId: info.appId, // 必填，公众号的唯一标识
			timestamp: info.timeStamp, // 必填，生成签名的时间戳zhifu
			nonceStr: info.nonceStr, // 必填，生成签名的随机串
			signature: info.paySign, // 必填，签名
			jsApiList: ["chooseWXPay"] // 必填，需要使用的JS接口列表
		})
		jweixin.ready(() => {
			jweixin.chooseWXPay({
				timestamp: info.timeStamp,
				nonceStr: info.nonceStr,
				package: info.package,
				signType: info.signType,
				paySign: info.paySign, // 支付签名
				success: (ret) => { // 支付成功后的回调函数
					uni.showToast({
						title: '支付成功',
						icon: 'error',
						duration: 2000
					});
				},
				fail: (res) => {
					uni.showToast({
						title: '支付失败',
						icon: 'error',
						duration: 2000
					});
				}
			});
		});
	})
}
export const getUrlCode = (name) => { //截取code
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}
export const _pay = (item) => {
	uni.requestPayment({
		provider: 'wxpay',
		orderInfo: item,
		success: (res) => {
			uni.showToast({
				title: '支付成功',
				icon: 'error',
				duration: 2000
			});
		},
		fail: (err) => {
			console.log('支付失败:' + JSON.stringify(err));
			uni.showToast({
				title: '支付失败' + JSON.stringify(err),
				icon: 'error',
				duration: 2000
			});
		},
		complete: (err1) => {
			console.log('结束回调' + JSON.stringify(err1));
		}
	});

}
